import { GameComponent } from "@sapi-game/gameComponent";
import { Game } from "@sapi-game/main";
import { Duration } from "@sapi-game/utils";
import { BlockedMainState } from "../states/mainState";

export class BlockedInCombatDaemon extends GameComponent<BlockedMainState> {
    override onAttach(): void {
        //循环
        this.subscribe(
            Game.events.interval,
            () => {
                this.checkEnd();
            },
            new Duration(20)
        );
    }

    checkEnd() {
        // 获取每个队伍中存活且有效的玩家列表
        const activeTeams = this.context.teams.filter((team) =>
            team.find((player) => player.live > 0 && player.isValid)
        );

        // 如果只剩一个或没有活队伍，结束游戏
        if (activeTeams.length <= 1) {
            this.state.end(activeTeams[0]);
        }
    }
}
